package cmp.viewer.panels;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.AbstractTableModel;

import util.ui.UserPrompt;

import cmp.main.Globals;
import cmp.viewer.ViewerFrame;

public class ResultPanel extends JPanel {
	private static final long serialVersionUID = -4532933089334778200L;

	public ResultPanel(ViewerFrame parentFrame, String [] columnLabels) {
		theParentFrame = parentFrame;
		setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBackground(Globals.BGCOLOR);
		colNames = columnLabels;
		
		theTable = new JTable();
		theTable.getTableHeader().setBackground(Globals.BGCOLOR);
		theTable.setColumnSelectionAllowed( false );
		theTable.setCellSelectionEnabled( false );
		theTable.setRowSelectionAllowed( true );
		theTable.setShowHorizontalLines( false );
		theTable.setShowVerticalLines( true );	
		theTable.setIntercellSpacing ( new Dimension ( 1, 0 ) );		
		rowData = new Vector<String []>();
		panels = new Vector<JPanel> ();
		theTable.setModel(new ResultsTableModel());
		theTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				updateButtons();
				if (e.getClickCount() == 2) {
					int row = theTable.getSelectedRow();
					theParentFrame.setSelection(panels.get(row));
				}
			}
		});
		
		scroll = new JScrollPane(theTable);
		scroll.setBorder( null );
		scroll.setPreferredSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize()); // force table to use all space
		scroll.setAlignmentX(Component.LEFT_ALIGNMENT);
		scroll.getViewport().setBackground(Globals.BGCOLOR);
				
		add(addButtonPanel());
		add(Box.createVerticalStrut(30));
		add(addLabelPanel());
		add(scroll);
	}
	
	public int getNumColumns() { return colNames.length; }
	
	public void addResult(JPanel theNewPanel, String name, String summary) {
		String [] temp = new String[2];
		temp[0] = name;
		temp[1] = summary;
		rowData.add(temp);
		panels.add(theNewPanel);
//		theTable.revalidate();
		updateButtons();
	}
		
	private void removeSelectedPanels(int [] selections) {
		for(int x=selections.length-1; x>=0; x--) {
			theParentFrame.removePanelFromMenuOnly(panels.get(selections[x]));
			panels.remove(selections[x]);
			rowData.remove(selections[x]);
		}
		theTable.clearSelection();
		theTable.revalidate();
		updateButtons();
	}
	
	public void removePanel(JPanel targetPanel) {
		for(int x=0; x<panels.size(); x++) {
			if(panels.get(x).equals(targetPanel)) {
				panels.remove(x);
				rowData.remove(x);
				theTable.revalidate();
				updateButtons();
				return;
			}
		}
	}
	
	public void renamePanel(JPanel targetPanel, String newName, String newSummary) {
		for(int x=0; x<theTable.getRowCount(); x++) {
			if(panels.get(x).equals(targetPanel)) {
	        	rowData.elementAt(x)[0] = newName;
	        	rowData.elementAt(x)[1] = newSummary;
				theTable.repaint();
				updateButtons();
				return;
			}
		}
	}
	
	private void updateButtons() {		
		if(theTable.getRowCount() > 0)
			btnRemoveAllPanels.setEnabled(true);
		else
			btnRemoveAllPanels.setEnabled(false);
	}
	
	private JPanel addButtonPanel() {
		JPanel thePanel = new JPanel();
		thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.LINE_AXIS));
		thePanel.setBackground(Globals.BGCOLOR);
		
		btnRemoveAllPanels = new JButton("Remove All");
		btnRemoveAllPanels.setEnabled(false);
		btnRemoveAllPanels.setBackground(Globals.BGCOLOR);
		btnRemoveAllPanels.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				theTable.selectAll();
				removeSelectedPanels(theTable.getSelectedRows());
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBackground(Globals.HELPCOLOR);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserPrompt.displayHTMLResourceHelp(theParentFrame, "Results Help", "html/viewMultiTCW/ResultPanel.html");
			}
		});
		
		thePanel.add(btnRemoveAllPanels);
		thePanel.add(Box.createHorizontalStrut(5));
		thePanel.add(btnHelp);
		
		thePanel.setMaximumSize(thePanel.getPreferredSize());
		thePanel.setAlignmentX(LEFT_ALIGNMENT);
		
		return thePanel;
	}
	
	private JPanel addLabelPanel() {
		JPanel thePanel = new JPanel();
		thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.PAGE_AXIS));
		thePanel.setBackground(Globals.BGCOLOR);
		
		JLabel headerLine = new JLabel("List Results");
		headerLine.setAlignmentX(LEFT_ALIGNMENT);
		JTextArea instructions = new JTextArea(	"Removing all results here  also removes them from the left panel\n" +
												"Double-click to switch to highlighted result");
				
		instructions.setEditable(false);
		instructions.setBackground(getBackground());
		instructions.setAlignmentX(LEFT_ALIGNMENT);
		
		thePanel.add(headerLine);
		thePanel.add(Box.createVerticalStrut(5));
		thePanel.add(instructions);
		thePanel.setMaximumSize(thePanel.getPreferredSize());
		thePanel.setAlignmentX(LEFT_ALIGNMENT);
		thePanel.add(Box.createVerticalStrut(10));
		
		return thePanel;
	}
	//Needed for summary updates
	Thread updateThread = null;

//	private JButton btnRemoveSelectedPanels = null;
	private JButton btnRemoveAllPanels = null;
	private JButton btnHelp = null;
	
    private JTable theTable = null;
	private JScrollPane scroll = null;

	private ViewerFrame theParentFrame = null;
	private String[] colNames = null;
	private Vector<String []> rowData = null;
	private Vector<JPanel> panels = null;

	private class ResultsTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 774460555629612058L;

		public int getColumnCount() {
            return colNames.length;
        }

        public int getRowCount() {
            return rowData.size();
        }
        
        public Object getValueAt(int row, int col) {
            String [] r = rowData.elementAt(row);
            return r[col];
        }
        
        public String getColumnName(int col) {
            return colNames[col];
        }        
	}


}

